<?php
import('iforum.web.iForumWebAppContext');
import("izz.service.ServiceLocator");
import('iforum.common.Array2Xml');
import('iforum.common.IforumRedirect');
import('iforum.common.IforumError');

/**
 * ActionRunner
 * 
 * @package  iforum.action
 * @author   iCer <iCer@justdn.com>
 * @version  $Id: ActionRunner.php,v 1.3 2005/04/06 04:07:55 icer Exp $
 */
class ActionRunner
{
	var $context;
	var $_resource;
	var $_tpl;
	var $_style;
	var $action;
	
	function ActionRunner()
	{
		$this->__construct();
	}
	
	function __construct()
	{
		$this->context = Singleton::instance('iForumWebAppContext');
	}
	
	function getStyleFolder()
	{
		return $this->_style;
	}
	
	function setStyleFolder($folder)
	{
		$this->_style = $folder;
	}
	

	function getResource()
	{
		$styleFolder = $this->getStyleFolder();
		$request = $this->context->getRequest();
		$this->action = $action = $request->getParameter('act');
		$tpl = ROOT_PATH.'/tpl/'.$styleFolder.'/'.$action.'.php';

		if (!file_exists($tpl))
		{
			die('ActionRunner:Can\'t find templete file:'.$tpl);
		}
		
		
		$fileTime = filemtime($tpl);
		$compiledFolder = ROOT_PATH.'/tpl_c/'.$styleFolder;
		
		$compiledResource = $compiledFolder.'/'.$action.'_r'.'.php';//such as Idx_r.php
		$compiledTemplete = $compiledFolder.'/'.$action.'_t'.'.xsl';//such as Idx_t.xsl

		if (!file_exists($compiledResource) ||
			!file_exists($compiledTemplete) ||
			filemtime($compiledResource) != $fileTime || 
			filemtime($compiledTemplete) != $fileTime)
		{
			import('iforum.common.TplComplier');
			TplComplier::complier($tpl);
		}
		
		return $compiledResource;
		
	}
	
	function loadResource($tpl)
	{
		$result =array();
		$resource = unserialize(file_get_contents($tpl));
		//print_r($resource);

		foreach($resource as $entry)
		{
			$service = ServiceLocator::getService($entry['service'].'Service');
			$service->param = $entry['param'];
			if (is_subclass_of($service, "RetrieveService") || is_subclass_of($service, "RetrieveTransactionService"))
			{
				if($service->param['name'] != "" || isset($service->param['name']))
				{
					$result[$service->param['name']] =  $service->getData();
				}
				else 
				{
					$result[$entry['service']] = $service->getData();
				}
				
			}
			elseif (is_subclass_of($service, "TransactionService"))
			{
				$service->execute();
			}
		}

		return $result;		
	}
	
	function run()
	{
		$result = $this->loadResource($this->getResource());	

		$result['action'] = $this->action;
		$xb = new Array2Xml();
		
		$pi = array(
			array(
				'xml-stylesheet',
				'type="text/xsl" href="tpl_c/'.$this->_style.'/'.$this->action.'_t.xsl"'
				)
		);
		
		if (IforumError::haveError())
		{
			$error = IforumError::getErrorInfo();
			$result = $xb->parse('error', $error, $pi);
		}
		elseif (IforumRedirect::needRedirect())
		{
			$redirect = IforumRedirect::getRedirectInfo();
			$result = $xb->parse('redirect', $redirect, $pi);
		}
		else 
		{
			$result = $xb->parse('result', $result, $pi);
		}

		ob_clean();
		header('Content-type: text/xml;');
		$xb->output();

	}
}


?>